package com.peaker.advertisingpro.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.util.SparseArray;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.peaker.advertisingpro.R;
import com.peaker.advertisingpro.entity.DataBean;
import com.peaker.advertisingpro.listener.OnPlayCompletionListener;
import com.peaker.advertisingpro.viewholder.ImageHolder;
import com.peaker.advertisingpro.viewholder.TitleHolder;
import com.peaker.advertisingpro.viewholder.VideoHolder;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.util.BannerUtils;

import java.util.List;

/**
 * 自定义布局,多个不同UI切换
 */
public class MultipleTypesAdapter extends BannerAdapter<DataBean, RecyclerView.ViewHolder> {
    private Context context;
    private SparseArray<RecyclerView.ViewHolder> mVHMap = new SparseArray<>();
    private OnPlayCompletionListener onPlayCompletionListener;

    public MultipleTypesAdapter(Context context, List<DataBean> mDatas) {
        super(mDatas);
        this.context = context;

    }
    public   void  setOnPlayCompletionListener(OnPlayCompletionListener onPlayCompletionListener){
        this.onPlayCompletionListener=onPlayCompletionListener;
}
    @Override
    public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
            case 2:
                return new VideoHolder(BannerUtils.getView(parent, R.layout.banner_video));
            case 3:
                return new TitleHolder(BannerUtils.getView(parent, R.layout.banner_title));
            default:
                break;
        }
        return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
    }

    @Override
    public int getItemViewType(int position) {
        return getData(getRealPosition(position)).viewType;
    }

    @Override
    public void onBindView(RecyclerView.ViewHolder holder, DataBean data, int position, int size) {
        int viewType = holder.getItemViewType();
        switch (viewType) {
            case 1:
                ImageHolder imageHolder = (ImageHolder) holder;
                mVHMap.append(position, imageHolder);
//                imageHolder.imageView.setImageResource(data.imageRes);
                Glide.with(holder.itemView)
                        .load(data.imageUrl)

//                        .thumbnail(Glide.with(holder.itemView).load(R.drawable.ic_launcher_background))
                        .into(imageHolder.imageView);
                break;
            case 2:
                VideoHolder videoHolder = (VideoHolder) holder;
                mVHMap.append(position, videoHolder);

                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                videoHolder.player.setVideoPath(data.imageUrl);

                if (!videoHolder.player.isPlaying()) {
                    videoHolder.player.start();
                }

                videoHolder.player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                        if(mDatas.size()==1){
                        mp.setLooping(true);
                        }
                    }
                });
                videoHolder.player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        onPlayCompletionListener.OnCompletion(position,2);
                    }
                });
                break;

            default:
                break;
        }
    }

    public SparseArray<RecyclerView.ViewHolder> getVHMap() {
        return mVHMap;
    }


}
